package com.ruoyi.jiaopei.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.jiaopei.mapper.TbCurriculumMapper;
import com.ruoyi.jiaopei.mapper.TbStudentMapper;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jiaopei.domain.bo.TbSchedulingBo;
import com.ruoyi.jiaopei.domain.vo.TbSchedulingVo;
import com.ruoyi.jiaopei.domain.TbScheduling;
import com.ruoyi.jiaopei.mapper.TbSchedulingMapper;
import com.ruoyi.jiaopei.service.ITbSchedulingService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 排期Service业务层处理
 *
 * @author macy
 * @date 2024-04-11
 */
@RequiredArgsConstructor
@Service
public class TbSchedulingServiceImpl implements ITbSchedulingService {

    private final TbSchedulingMapper baseMapper;
    private final TbStudentMapper studentMapper;
    private final TbCurriculumMapper curriculumMapper;
    private final ISysUserService sysUserService;

    /**
     * 查询排期
     */
    @Override
    public TbSchedulingVo queryById(Long id){
        TbSchedulingVo schedulingVo=baseMapper.selectVoById(id);
        if(null!=schedulingVo){
            if(null!=schedulingVo.getStudentId()){
                schedulingVo.setStudentVo(studentMapper.selectVoById(schedulingVo.getStudentId()));
            }
            if(null!=schedulingVo.getCurriculumId()){
                schedulingVo.setCurriculumVo(curriculumMapper.selectVoById(schedulingVo.getCurriculumId()));
            }
            if(null!=schedulingVo.getTeacherId()){
                schedulingVo.setTeacherVo(sysUserService.selectUserById(schedulingVo.getTeacherId()));
            }
        }
        return schedulingVo;
    }

    /**
     * 查询排期列表
     */
    @Override
    public TableDataInfo<TbSchedulingVo> queryPageList(TbSchedulingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbScheduling> lqw = buildQueryWrapper(bo);
        Page<TbSchedulingVo> result = baseMapper.selectPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询排期列表
     */
    @Override
    public List<TbSchedulingVo> queryList(TbSchedulingBo bo) {
        LambdaQueryWrapper<TbScheduling> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbScheduling> buildQueryWrapper(TbSchedulingBo bo) {

        QueryWrapper<TbScheduling> lqw1 = Wrappers.query();

        lqw1.eq(StringUtils.isNotBlank(bo.getStudentName()),"s.name", bo.getStudentName());
        lqw1.eq(StringUtils.isNotBlank(bo.getCurriculumName()),"c.name", bo.getCurriculumName());
        lqw1.eq(StringUtils.isNotBlank(bo.getTeacherName()),"u.nick_name", bo.getTeacherName());
        lqw1.eq(StringUtils.isNotBlank(bo.getStatus()),"t.status", bo.getStatus());
        lqw1.eq(bo.getTeacherId() != null,"t.teacher_id", bo.getTeacherId());


        LambdaQueryWrapper<TbScheduling> lqw = lqw1.lambda();

        lqw.eq(bo.getSection() != null, TbScheduling::getSection, bo.getSection());
        lqw.eq(StringUtils.isNotBlank(bo.getWeek()), TbScheduling::getWeek, bo.getWeek());
        lqw.eq(StringUtils.isNotBlank(bo.getCourseDuration()), TbScheduling::getCourseDuration, bo.getCourseDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getStartHour()), TbScheduling::getStartHour, bo.getStartHour());
        lqw.eq(StringUtils.isNotBlank(bo.getEndHour()), TbScheduling::getEndHour, bo.getEndHour());
        lqw.eq(bo.getLessonHours() != null, TbScheduling::getLessonHours, bo.getLessonHours());
        lqw.eq(bo.getStudentId() != null, TbScheduling::getStudentId, bo.getStudentId());
        lqw.eq(bo.getCurriculumId() != null, TbScheduling::getCurriculumId, bo.getCurriculumId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), TbScheduling::getRemarks, bo.getRemarks());
        lqw.eq(bo.getCreatedDate() != null, TbScheduling::getCreatedDate, bo.getCreatedDate());
        return lqw;
    }

    /**
     * 新增排期
     */
    @Override
    public Boolean insertByBo(TbSchedulingBo bo) {
        TbScheduling add = BeanUtil.toBean(bo, TbScheduling.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改排期
     */
    @Override
    public Boolean updateByBo(TbSchedulingBo bo) {
        TbScheduling update = BeanUtil.toBean(bo, TbScheduling.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbScheduling entity){
        if(null==entity.getId()){
            entity.setCreatedDate(new Date());
        }
    }

    /**
     * 批量删除排期
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
